package com.experience.util.http;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

import net.sf.json.JSONObject;

/**
  * 
  * @ClassName: httpClientUtil 
  * @Description: TODO(post和get请求) 
  * @author FANGDAJI 
  * @date 2016年11月26日 下午12:22:03 
  * commons-httpclient-3.0.1.jar
  * commons-lang-2.6.jar
  * commons-codec-1.9.jar
  * json-lib-2.1-jdk15.jar
  */
public class httpClientUtil {
	public static HttpMethod getMethod(String url,String param) throws IOException{
        GetMethod get = new GetMethod(url+"?"+param);
        get.releaseConnection();
        return get;
    }
	
	public static HttpMethod postMethod(String url,NameValuePair[] param) throws IOException{
        PostMethod post = new PostMethod(url);
        post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8"); 
        post.setRequestBody(param);
        post.releaseConnection();
        return post;
    }
	
	/**
	 * 
	 * @Title: getToJson 
	 * @Description: TODO(get请求返回String) 
	 * @param @param host
	 * @param @param url
	 * @param @param param
	 * @param @return   
	 * @return JSONObject    
	 * @throws
	 */
	public static String getToString(String host,String url,String param){
		HttpClient httpClient = new HttpClient();
		httpClient.getHostConfiguration().setHost(host,80,"http");
		HttpMethod method;
		try {
			method = getMethod(url,param);
			httpClient.executeMethod(method);
			String response = method.getResponseBodyAsString();
			return response;
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
    }
	
	/**
	 * 
	 * @Title: getToJson 
	 * @Description: TODO(get请求返回json) 
	 * @param @param host
	 * @param @param url
	 * @param @param param
	 * @param @return   
	 * @return JSONObject    
	 * @throws
	 */
	public static JSONObject getToJson(String host,String url,String param){
		HttpClient httpClient = new HttpClient();
		httpClient.getHostConfiguration().setHost(host,80,"http");
		HttpMethod method;
		try {
			method = getMethod(url,param);
			httpClient.executeMethod(method);
			String response = method.getResponseBodyAsString();
			JSONObject jsonObject = JSONObject.fromObject(response);
			return jsonObject;
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
    }
	
	/**
	 * 
	 * @Title: postToJson 
	 * @Description: TODO(post请求返回String) 
	 * @param @param host
	 * @param @param url
	 * @param @param param
	 * @param @return   
	 * @return JSONObject    
	 * @throws
	 */
	public static String postToString(String host,String url,NameValuePair[] param){
		HttpClient httpClient = new HttpClient();
		httpClient.getHostConfiguration().setHost(host,80,"http");
		HttpMethod method;
		try {
			method = postMethod(url,param);
			httpClient.executeMethod(method);
			String response = method.getResponseBodyAsString();
			return response;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
    }
	
	/**
	 * 
	 * @Title: postToJson 
	 * @Description: TODO(post请求返回json) 
	 * @param @param host
	 * @param @param url
	 * @param @param param
	 * @param @return   
	 * @return JSONObject    
	 * @throws
	 */
	public static JSONObject postToJson(String host,String url,NameValuePair[] param){
		HttpClient httpClient = new HttpClient();
		httpClient.getHostConfiguration().setHost(host,80,"http");
		HttpMethod method;
		try {
			method = postMethod(url,param);
			httpClient.executeMethod(method);
			String response = method.getResponseBodyAsString();
			JSONObject jsonObject = JSONObject.fromObject(response);
			return jsonObject;
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
    }
	
	
	
	
	
	public static void main(String[] args) {
		//传值
		NameValuePair[] param = { 
			new NameValuePair("app_id", "10")
		};
		System.out.println(httpClientUtil.postToString("www.baidu.com", "", param));
	}

}
